
-----------------COUNT------
DROP FUNCTION IF EXISTS public."widget_GetBookedLabCountByDate"(date, integer);

CREATE OR REPLACE FUNCTION public."widget_GetBookedLabCountByDate"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select COUNT(*) from "NewLabBookingHeader" 
where "CreatedDate"::date="fromDate" and "Active" is true
and case when "referenceId" is null then 1=1 else "DoctorId"= "referenceId" end;

end
$BODY$;
----------------------CASH--------------

DROP FUNCTION IF EXISTS public."widget_Labs_OverAll_CashTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_Labs_OverAll_CashTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end
 
)
,LabAmount as ( 
select a."AccountId",sum(a."Cash") "LabCash",sum(a."Card") "LabCard" ,
	sum(a."Cash")+sum(a."Card")"LabAmount" 
from(
select 	a."AccountId",	

	case when lh."PayTypeId"=1 then  coalesce(ar."NetAmount",0) else 0 end "Cash"
	,case when lh."PayTypeId"=2 then  coalesce(ar."NetAmount",0) else 0 end "Card"
from "Account" a
	join "NewLabBookingHeader" lh on  lh."CreatedBy" = a."AccountId" and lh."Active" <> false
join "NewLabBookingDetail" ar on lh."NewLabBookingHeaderId"=ar."NewLabBookingHeaderId" and ar."LabBookingStatusId" <> 2
	
join "Provider" pr on pr."ProviderId"= lh."DoctorId"
join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId"
join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
where case when "fromDate" is null then 1=1 else lh."CreatedDate"::date = "fromDate" end 
	and case when "locationId" is null then 1=1 else lh."LocationId"= "locationId" and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end 
)a
group by a."AccountId" )

select distinct 
sum(coalesce(lb."LabCash",0)) "TotalCash"

from  accountdata a

left join LabAmount lb on lb."AccountId"=a."AccountId"
;
end
$BODY$;

---------------------CARD--------
DROP FUNCTION IF EXISTS public."widget_Labs_OverAll_CardTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_Labs_OverAll_CardTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
	join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end
)
,LabAmount as ( 
select a."AccountId",sum(a."Cash") "LabCash",sum(a."Card") "LabCard" ,sum(a."Cash")+sum(a."Card")"LabAmount"
	from(
select 	a."AccountId",	

	case when lh."PayTypeId"=1 then  coalesce(ar."NetAmount",0) else 0 end "Cash"
	,case when lh."PayTypeId"=2 then  coalesce(ar."NetAmount",0) else 0 end "Card"
from "Account" a
--join "LabBookingHeader" ar on ar."CreatedBy" = a."AccountId" and ar."Active" <> false
		join "NewLabBookingHeader" lh on  lh."CreatedBy" = a."AccountId" and lh."Active" <> false
join "NewLabBookingDetail" ar on lh."NewLabBookingHeaderId"=ar."NewLabBookingHeaderId" and ar."LabBookingStatusId" <> 2
	
join "Provider" pr on pr."ProviderId"=lh."DoctorId"
join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId"
join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
where case when "fromDate" is null then 1=1 else lh."CreatedDate"::date = "fromDate" end 
	and case when "locationId" is null then 1=1 else lh."LocationId"= "locationId" and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end 
)a
group by a."AccountId" )

select distinct 
sum(coalesce(lb."LabCard",0)) "TotalCard"

from  accountdata a

left join LabAmount lb on lb."AccountId"=a."AccountId"
;
end
$BODY$;

--------------UPI---------
DROP FUNCTION IF EXISTS public."widget_Labs_OverAll_UPITotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_Labs_OverAll_UPITotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("TotalUpi" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end
 
)
,LabAmount as ( 
select a."AccountId",sum(a."Upi") "LabUpi"
from(
select 	a."AccountId",	
	case when lh."PayTypeId"=3 then  coalesce(ar."NetAmount",0) else 0 end "Upi"
	
from "Account" a
--join "LabBookingHeader" ar on ar."CreatedBy" = a."AccountId" and ar."Active" <> false
	join "NewLabBookingHeader" lh on  lh."CreatedBy" = a."AccountId" and lh."Active" <> false
join "NewLabBookingDetail" ar on lh."NewLabBookingHeaderId"=ar."NewLabBookingHeaderId" and ar."LabBookingStatusId" <> 2
	
join "Provider" pr on pr."ProviderId"= lh."DoctorId" --ar."ProviderId"
join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId"
join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
where case when "fromDate" is null then 1=1 else lh."CreatedDate"::date = "fromDate" end 
	and case when "locationId" is null then 1=1 else lh."LocationId"= "locationId" and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end 
)a
group by a."AccountId" )

select distinct 
sum(coalesce(lb."LabUpi",0)) "TotalUpi"
from  accountdata a
left join LabAmount lb on lb."AccountId"=a."AccountId"
;
end
$BODY$;

------online----
DROP FUNCTION IF EXISTS public."widget_Labs_OverAll_OnlinePaymentTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_Labs_OverAll_OnlinePaymentTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end
 
)
,LabAmount as ( 
select a."AccountId",sum(a."OnlinePayment") "LabOnlinePayment"
from(
select 	a."AccountId",	
	case when lh."PayTypeId"=4 then  coalesce(ar."NetAmount",0) else 0 end "OnlinePayment"
	
from "Account" a
--join "LabBookingHeader" ar on ar."CreatedBy" = a."AccountId" and ar."Active" <> false
	join "NewLabBookingHeader" lh on  lh."CreatedBy" = a."AccountId" and lh."Active" <> false
join "NewLabBookingDetail" ar on lh."NewLabBookingHeaderId"=ar."NewLabBookingHeaderId" and ar."LabBookingStatusId" <> 2
	
join "Provider" pr on pr."ProviderId"= lh."DoctorId" --ar."ProviderId"
join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId"
join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
where case when "fromDate" is null then 1=1 else lh."CreatedDate"::date = "fromDate" end 
	and case when "locationId" is null then 1=1 else lh."LocationId"= "locationId" and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end 
)a
group by a."AccountId" )

select distinct 
sum(coalesce(lb."LabOnlinePayment",0)) "TotalOnlinePayment"
from  accountdata a
left join LabAmount lb on lb."AccountId"=a."AccountId"
;
end
$BODY$;
----------------Cheque-----------
DROP FUNCTION IF EXISTS public."widget_Labs_OverAll_ChequeTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_Labs_OverAll_ChequeTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end
 
)
,LabAmount as ( 
select a."AccountId",sum(a."Cheque") "LabCheque"
from(
select 	a."AccountId",	
	case when lh."PayTypeId"=5 then  coalesce(ar."NetAmount",0) else 0 end "Cheque"
	
from "Account" a
--join "LabBookingHeader" ar on ar."CreatedBy" = a."AccountId" and ar."Active" <> false
	join "NewLabBookingHeader" lh on  lh."CreatedBy" = a."AccountId" and lh."Active" <> false
join "NewLabBookingDetail" ar on lh."NewLabBookingHeaderId"=ar."NewLabBookingHeaderId" and ar."LabBookingStatusId" <> 2
	
join "Provider" pr on pr."ProviderId"= lh."DoctorId" --ar."ProviderId"
join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId"
join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
where case when "fromDate" is null then 1=1 else lh."CreatedDate"::date = "fromDate" end 
	and case when "locationId" is null then 1=1 else lh."LocationId"= "locationId" and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end 
)a
group by a."AccountId" )

select distinct 
sum(coalesce(lb."LabCheque",0)) "TotalCheque"
from  accountdata a
left join LabAmount lb on lb."AccountId"=a."AccountId"
;
end
$BODY$;
------------PayTm-----
DROP FUNCTION IF EXISTS public."widget_Labs_OverAll_PaytmTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_Labs_OverAll_PaytmTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end
 
)
,LabAmount as ( 
select a."AccountId",sum(a."Paytm") "LabPaytm"
from(
select 	a."AccountId",	
	case when lh."PayTypeId"=6 then  coalesce(ar."NetAmount",0) else 0 end "Paytm"
	
from "Account" a
--join "LabBookingHeader" ar on ar."CreatedBy" = a."AccountId" and ar."Active" <> false
	join "NewLabBookingHeader" lh on  lh."CreatedBy" = a."AccountId" and lh."Active" <> false
join "NewLabBookingDetail" ar on lh."NewLabBookingHeaderId"=ar."NewLabBookingHeaderId" and ar."LabBookingStatusId" <> 2
	
join "Provider" pr on pr."ProviderId"=lh."DoctorId" -- ar."ProviderId"
join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId"
join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
where case when "fromDate" is null then 1=1 else lh."CreatedDate"::date = "fromDate" end 
	and case when "locationId" is null then 1=1 else lh."LocationId"= "locationId" and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end 
)a
group by a."AccountId" )

select distinct 
sum(coalesce(lb."LabPaytm",0)) "TotalPaytm"
from  accountdata a
left join LabAmount lb on lb."AccountId"=a."AccountId"
;
end
$BODY$;
----------
----------COUNT-Proper_______
DROP FUNCTION IF EXISTS public."widget_GetBookedLabCountByDate"(date, integer);

CREATE OR REPLACE FUNCTION public."widget_GetBookedLabCountByDate"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

--select * from "NewLabBookingHeader" 
select COUNT(*) from "NewLabBookingHeader" LH
	join "NewLabBookingDetail" LD on LD."NewLabBookingHeaderId"=LH."NewLabBookingHeaderId"
	where LH."CreatedDate"::date="fromDate" and LH."Active" is true and LD."LabBookingStatusId"!=2
--where "CreatedDate"::date="fromDate" and "Active" is true
and case when "referenceId" is null then 1=1 else LH."DoctorId"= "referenceId" end;

end
$BODY$;
--------------------------
--------------



